package com.ossean.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EDDHandler {
	
	//提取主副描述信息1——（）类型
	public static List<String> getMainAndViceDescriptionByBracket(String str){
		List<String> result = new ArrayList<String>();
		int index1 = -1;
		int index2 = -1;
		String strInBracket = null;
		
		index1 = str.indexOf("(");
		index2 = str.indexOf(")");
		if(index1 > 0){
			result.add(str.substring(0,index1).trim());
			if(index2 > index1){
				strInBracket = str.substring(index1 + 1, index2);
			}
		}else{
			index1 = str.indexOf("（");
			index2 = str.indexOf("）");
			if(index1 > 0){
				result.add(str.substring(0,index1).trim());
				if(index2 > index1){
					strInBracket = str.substring(index1 + 1, index2);
				}
			}else{
				index1 = str.indexOf("[");
				index2 = str.indexOf("]");
				if(index1 > 0){
					result.add(str.substring(0,index1).trim());
					if(index2 > index1){
						strInBracket = str.substring(index1 + 1, index2);
					}
				}else{
					index1 = str.indexOf("【");
					index2 = str.indexOf("】");
					if(index1 > 0){
						result.add(str.substring(0,index1).trim());
						if(index2 > index1){
							strInBracket = str.substring(index1 + 1, index2);
						}
					}else{
						index1 = str.indexOf("{");
						index2 = str.indexOf("}");
						if(index1 > 0){
							result.add(str.substring(0,index1).trim());
							if(index2 > index1){
								strInBracket = str.substring(index1 + 1, index2);
							}
						}else{
							result.add(str.trim());
						}
					}
				}
			}
		}
		if(strInBracket != null){
			result.add(strInBracket.trim());//暂时没有对括号中的数据进行常用词排除处理
		}
		
		return result;
	}
	
	
	//提取主副描述信息2——known as
	public static List<String> getMainAndViceDescriptionByKnowAs(List<String> strs){
		List<String> result = new ArrayList<String>();
		for(String str:strs){
			boolean flag = false;
			if(str == null)
				continue;
			List<String> knownAss = FileReader.read("./files/known_as.txt");
			for(String tmp:knownAss){
				int index = str.toLowerCase().indexOf(tmp);
				if(index > 0){
					String tmp2 = str.substring(index + tmp.length()).trim();
					String[] resultTmp = tmp2.split(" and ");
					for(String s:resultTmp){
						String[] resultTmp2 = s.split(" or ");
						for(String s2:resultTmp2){
							result.add(s2.trim());
						}
					}
					flag = true;
					break;
				}
			}
			if(flag == false){
				//表示没有known as之类的短语
				result.add(str.trim());
			}
		}
		return result;
	}
	
	
	//去除定语描述信息1——,
	public static List<String> removeComma(List<String> strs){
		for(int i = 0; i < strs.size(); i++){
			String str = strs.get(i);
			int index1 = str.indexOf(",");
			if(index1 > 0){
				strs.remove(i);
				i--;
				continue;
			}
			int index2 = str.indexOf("，");
			if(index2 > 0){
				strs.remove(i);
				i--;
				continue;
			}
		}
		return strs;
		
	}
	

	//去除定语描述信息2——The
	public static List<String> removeDemonstrativeWords(List<String> strs){
		List<String> demonstrativeWords = FileReader.read("./files/demonstrativeWord.txt");
		List<String> results = new ArrayList<String>();
		for(String str:strs){
			int lastIndex = -1;
			for(int i = 0; i < demonstrativeWords.size(); i++){
				String dWord = demonstrativeWords.get(i);
				int indexTmp = -1;
				if((indexTmp = str.lastIndexOf(dWord)) > lastIndex){
					lastIndex = indexTmp + dWord.length();
				}
			}
			if(lastIndex > 0){
				String result = str.substring(lastIndex).trim();
				results.add(result);
				continue;
			}
			results.add(str.trim());
		}
		
		return results;
	}
	
	
	//去除从句描述信息
	public static List<String> removeArrtibutiveClause(List<String> strs){
		for(int i = 0; i < strs.size(); i++){
			String str = strs.get(i);
			//去除which is/that is/what is这样的从句
			String[] acs = {" that"," which"," what"};
			for(String ac:acs){
				str = str.trim();
				int index = str.lastIndexOf(ac);
				if(index > 0){
					if(index + ac.length() >= str.length()){
						strs.remove(i);
						i--;
						break;
					}
				}
			}
		}
		return strs;
	}
	
	
	//去除指示代词，项目描述词以及两者的笛卡尔积
	//去除指示代词
	public static List<String> removePronoun(List<String> strs){
		for(int i = 0; i < strs.size(); i++){
			String str = strs.get(i).trim();
			List<String> pronouns = FileReader.read("./files/pronoun.txt");
			for(int j = 0; j < pronouns.size(); j++){
				String pronoun = pronouns.get(j);
				if((str.toLowerCase()).equals(pronoun.toLowerCase())){
					strs.remove(i);
					i--;
					break;
				}
			}
		}
		return strs;
	}
	
	
	//去除项目常用词
	public static List<String> removeProject(List<String> strs){
		for(int i = 0; i < strs.size(); i++){
			String str = strs.get(i).trim();
			List<String> nouns = FileReader.read("./files/project.txt");
			for(String noun:nouns){
				if((str.toLowerCase()).equals(noun.toLowerCase())){
					strs.remove(i);
					i--;
					break;
				}
			}
		}
		return strs;
	}
	
	//去除两者的笛卡尔积
	public static List<String> removePPCombine(List<String> strs){
		for(int i = 0; i < strs.size(); i++){
			String str = strs.get(i).trim();
			List<String> pronouns = FileReader.read("./files/pronoun.txt");
			List<String> nouns = FileReader.read("./files/project.txt");
			for(int k = 0; k < pronouns.size(); k++){
				boolean flag = false;//用于判断是不是删除了当前的字符串
				String pronoun = pronouns.get(k).toLowerCase();
				for(int j = 0; j < nouns.size(); j++){
					String noun = nouns.get(j).toLowerCase();
					Pattern pattern = Pattern.compile(pronoun+"\\s*"+noun);
					Matcher matcher = pattern.matcher(str.toLowerCase());
					if(matcher.find()){
						strs.remove(i);
						i--;
						flag = true;
						break;
					}
				}
				if(flag){
					break;
				}
			}
		}
		return strs;
	}
	
	//特殊字符处理 空字符串 引号 冒号
	//空字符串
	public static List<String> removeEmpty(List<String> strs){
		for(int i = 0; i < strs.size(); i++){
			String str = strs.get(i);
			Pattern pattern = Pattern.compile("^\\s*$");
			Matcher matcher = pattern.matcher(str);
			Boolean find = matcher.find();
			if(find){
				strs.remove(i);
				i--;
			}
		}
		return strs;
	}
	
	//引号的处理
	public static List<String> removeSemicolon(List<String> strs){
		List<String> result = new ArrayList<String>();
		for(int i = 0; i < strs.size(); i++){
			String str = strs.get(i);
			str = str.replaceAll("\"", " ").trim();
			str = str.replaceAll("“", " ").trim();
			str = str.replaceAll("”", " ").trim();
			str = str.replaceAll("`", " ").trim();
			//如果单引号是在开头和结尾 去除
			int index1 = str.indexOf("\'");
			int index2 = str.lastIndexOf("\'");
			if(index1 == 0 && index2 >= str.length() - 1){
				str = str.substring(1,index2).trim();
			}
			result.add(str);
		}
		return result;
	}
	
	//冒号处理（将冒号处理替换成特殊字符处理）
	public static List<String> removeSingleColon(List<String> strs){
		List<String> results = new ArrayList<String>();
		for(int i = 0; i < strs.size(); i++){
			String str = strs.get(i);
			//中文
			int indexChi = str.lastIndexOf("：");
			if(indexChi > 0){
				str = str.substring(indexChi + 1);
			}
			//英文
			Pattern pattern = Pattern.compile("\\w+(:::\\w+|::\\w+)+");
			Matcher matcher = pattern.matcher(str);
			if(matcher.find()){
				str = matcher.group(0);
				str = str.replaceAll(":::", " ");
				str = str.replaceAll("::", " ");
			}
			int indexEng = str.lastIndexOf(":");
			if(indexEng > 0){
				str = str.substring(indexEng + 1);
				results.add(str);
			}
			results.add(str);
		}
		return results;
	}
}
